package pa165.dominatingspecies.frontend.environment;

import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.tos.EnvironmentTo;
import pa165.dominatingspecies.frontend.pages.BasePage;
import pa165.dominatingspecies.frontend.security.Authorizer;
import pa165.dominatingspecies.service.impl.EnvironmentServiceImpl;

/**
 *
 * @author Stanislau Dubrouski
 */
@PreAuthorize("hasRole('WORKER')")
public class EnvironmentsPage extends BasePage {

    @SpringBean(name = "environmentService")
    private EnvironmentServiceImpl envService;

    public EnvironmentsPage() {
        initGui();
    }

    private void initGui() {
        addEnvironmentsModule();
        addNewEnvironmentLink();
    }

    private void addEnvironmentsModule() {
        ListView<EnvironmentTo> listView = new ListView<EnvironmentTo>("listview", createModelForEnvironments()) {
            @Override
            protected void populateItem(ListItem<EnvironmentTo> item) {
                item.add(new Label("id", new PropertyModel(item.getModel(), "id")));
                final EnvironmentTo env = (EnvironmentTo) item.getModelObject();
                item.add(new Label("name", new PropertyModel(item.getModel(), "name")));
                item.add(new Label("desc", new PropertyModel(item.getModel(), "description")));
                
                item.add(new EditEnvironmentLink("edit", env.getId()));
                item.add(new EnvironmentDetailsLink("details", env.getId()));
                
                Link deleteEnvLink = new DeleteEnvironmentLink("delete", item.getModelObject());
                Authorizer.authorize(deleteEnvLink);
                item.add(deleteEnvLink);

            }
        };
        add(listView);
    }

    private LoadableDetachableModel<List<EnvironmentTo>> createModelForEnvironments() {
        return new LoadableDetachableModel<List<EnvironmentTo>>() {
            @Override
            protected List<EnvironmentTo> load() {
                return envService.getAllEnvironments();
            }
        };
    }

    private void addNewEnvironmentLink() {
        Link addEnvLink = new Link("createEnvironmentLink") {
            @Override
            public void onClick() {
                setResponsePage(new CreateEnvironmentPage());
            }
        };
        Authorizer.authorize(addEnvLink);
        add(addEnvLink);
    }

    public void setEnvironmentService(EnvironmentServiceImpl envService) {
        this.envService = envService;
    }
}
